<!DOCTYPE html>
<script src="../../../../../resources/testharness.js"></script>
<script src="../../../../../resources/testharnessreport.js"></script>
<script src="../../../../../resources/gesture-util.js"></script>
<div id="testdiv" style="margin: 0px; width: 100px; height: 100px; background-color: blue;"></div>
<script>

// For the pinch inside the div.
var X_INSIDE = 50;
var Y_INSIDE = 40;
var SCALE_INSIDE = 1.5;
// For the pinch outside the div.
var X_OUTSIDE = 150;
var Y_OUTSIDE = 100;
var SCALE_OUTSIDE = 2.0;

var TOLERANCE = 0.001;
var MOUSE_INPUT = GestureSourceType.MOUSE_INPUT;

var t = async_test("Touchpad pinch can be prevented by wheel handlers");

var numWheelEventsSeen = 0;
var totalScaleChange = 1.0;

function wheelHandler(event) {
  numWheelEventsSeen++;

  var scaleChange = Math.exp(-event.deltaY / 100);
  totalScaleChange *= scaleChange;

  assert_true(event.cancelable);
  event.preventDefault();
}

document.getElementById('testdiv').addEventListener(
    'wheel', t.step_func(wheelHandler), {passive: false});

if (window.chrome && chrome.gpuBenchmarking) {
  window.onload = t.step_func(function() {
    assert_approx_equals(
        window.visualViewport.scale, 1.0, TOLERANCE);

    // Ensure the compositor is made aware of the handler before we send the
    // pinch.
    waitForCompositorCommit().then(t.step_func(function() {
      // Do a pinch in a region that prevents the event(s) from performing a
      // zoom.
      chrome.gpuBenchmarking.pinchBy(
          SCALE_INSIDE, X_INSIDE, Y_INSIDE, t.step_func(function() {
        assert_approx_equals(
            window.visualViewport.scale, 1.0, TOLERANCE);
        assert_greater_than(numWheelEventsSeen, 0);
        assert_approx_equals(totalScaleChange, SCALE_INSIDE, TOLERANCE);

        numWheelEventsSeen = 0;
        totalScaleChange = 1.0;

        // Do a pinch outside of this region and ensure that a zoom occurs, and
        // that it occurs around the pinch area.
        chrome.gpuBenchmarking.pinchBy(
            SCALE_OUTSIDE, X_OUTSIDE, Y_OUTSIDE, t.step_func_done(function() {
          assert_approx_equals(
              window.visualViewport.scale, SCALE_OUTSIDE, TOLERANCE);
          assert_approx_equals(window.visualViewport.offsetLeft, 75, TOLERANCE);
          assert_approx_equals(window.visualViewport.offsetTop, 50, TOLERANCE);
          assert_equals(numWheelEventsSeen, 0);
        }), undefined /* speed */, MOUSE_INPUT);
      }), undefined /* speed */, MOUSE_INPUT);
    }));
  });
} else {
  t.step(function() {
    assert_unreached('This test requires chrome.gpuBenchmarking.pinchBy');
  });
  t.done();
}

</script>
